/**
 * Created by Jie on 2017/2/4.
 */
var data = require('../../util/result');
var Menu = require('../../model/core/menu');

exports.index = function (req,res) {
    res.render('core/menu', {title: '菜单管理'});
};

exports.form = function (req,res) {
    var menu = new Menu();
    menu.one(req.params.id,function (err,result) {
        if(err){
            res.render('core/_form/_menu_form', {title: '菜单操作'});
            return;
        }
        res.render('core/_form/_menu_form', {title: '菜单操作',obj:result});
    })

};

exports.one = function (req,res) {
    var menu = new Menu();
    menu.one(req.params.id,function (err,result) {
        if(err){
            res.json(data.result(1,"数据异常"));
            return;
        }
        if((typeof result) == 'object'){
            res.json(data.result(0,"",result));
        }else{
            res.json(data.result(1,"数据异常"));
        }
    })
}

exports.insert = function (req,res) {
    var menu = new Menu();
    menu.insert(req.body,function (err,result) {
        if(err){
            res.json(data.result(1,"数据异常"));
            return;
        }
        if(result.insertId > 0){
            res.json(data.result(0,"数据操作成功"));
        }else{
            res.json(data.result(1,"数据异常"));
        }
    });
}

exports.edit = function (req,res) {
    var menu = new Menu();
    req.body.id = req.params.id;
    menu.edit(req.body,function (err,result) {
        if(err){
            res.json(data.result(1,"数据异常"));
            return;
        }
        if(result.affectedRows > 0){
            res.json(data.result(0,"数据操作成功"));
        }else{
            res.json(data.result(1,"数据异常"));
        }
    });
}

exports.del = function (req,res) {
    var menu = new Menu();
    req.body.id = req.params.id;
    menu.del(req.body,function (err,result) {
        if(err){
            res.json(data.result(1,"数据异常"));
            return;
        }
        if(result.affectedRows > 0){
            res.json(data.result(0,"数据操作成功"));
        }else{
            res.json(data.result(1,"数据异常"));
        }
    });
}

exports.list = function (req,res) {
    var menu = new Menu();
    var where = req.body;
    menu.list(where,function (err,result) {
        if(err){
            res.json(data.result(1,"数据异常"));
            return;
        }
        if((typeof result) == 'object'){
            res.json(result);
        }else{
            res.json(data.result(1,"数据异常"));
        }
    });
}

